package com.hehe.boot.controller;

import com.hehe.boot.pojo.Book;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.pojo.User;
import com.hehe.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class SignController {
    @Autowired
    private BookService bookService;

    private static final Map<Integer,String> Identity_Map = new HashMap();

    static {
        Identity_Map.put(1,"admin");
        Identity_Map.put(2,"doctor");
        Identity_Map.put(3,"user");
    }

    @PostMapping("login")
    public Result<Map<String,String>> SignIn(@RequestBody Book book){
        String username = book.getUsername();
        Book film = bookService.findBookByName(username);
        if (film != null && film.getPassword().equals(book.getPassword())){
            System.out.println("handler login");
            Map<String, String> resp = new HashMap<>();
            resp.put("identity",Identity_Map.get(film.getIdentity()));
            resp.put("relatedId",film.getRelatedId().toString());
            return Result.success();
        }else {
            return Result.error("fail to login");
        }
    }

    @DeleteMapping("delete")
    public Result<Boolean> Delete(@RequestBody Integer id){
        boolean flag = bookService.deleteById(id);
        if (flag == true){
            return Result.success();
        }else{
            return Result.error("fail to delete by id");
        }
    }
}
